<!DOCTYPE html>
<html>
<head>
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
	<style type="text/css">
		.bn {
            padding: 8px 20px;
            width: 100%;
            height: auto;
            margin: 0 auto;
            text-align: center;
            margin-top: 20px;
        }
	</style>
	<title>测试</title>
</head>
<body>
<div style="display: flex; flex-direction: column;">
	<button class="bn" onclick="showAlert()">显示弹窗</button>
	<button class="bn" onclick="loadNetStatu()">获取当前网络状态</button>
	<button class="bn" onclick="getGpsInfo()">获取gps信息</button>
	<button class="bn" onclick="writeUserInfo()">写入用户信息</button>
	<button class="bn" onclick="getUserInfo()">获取本地用户信息</button>
	<button class="bn" onclick="writBusTruck()">写入订单</button>
	<button class="bn" onclick="readBusTruck()">读取订单详情</button>
	<button class="bn" onclick="writeLogInfo()">写入日志</button>
	<button class="bn" onclick="startUpdate()">上报日志信息</button>
	<h4 id="text"></h4>
</div>
</body>
</body>

<script type='text/javascript'>
	function setupWebViewJsBridge(callback) {
        let isAndroid = Boolean(navigator.userAgent.match(/android/ig));
        let isIOS = Boolean(navigator.userAgent.match(/iphone|ipod|iOS/ig));
        if (window.WKWebViewJavascriptBridge) {
            return callback(window.WKWebViewJavascriptBridge);
        }
        if (window.WKWVJBCallbacks) {
            window.WKWVJBCallbacks.push(callback);
        } else {
            window.WKWVJBCallbacks = [callback];
        }
        if (isAndroid) {
            if (!window.InjectJavascript) {
                window.alert("ERROR: window.InjectJavascript if null!");
                return;
            }
            window.InjectJavascript.init();
        } else if (isIOS) {
            window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null);
        }
    }


    function showAlert() {
        window.alert("显示弹窗");
    }

    function loadNetStatu() {
        setupWebViewJsBridge(function(bridge) {
            bridge.callHandler("getNetState", null, function(data) {
                window.alert("显示网络：" + data);

            })
        })
    }

    function getGpsInfo() {
        setupWebViewJsBridge(function(bridge) {
            bridge.callHandler("getGpsInfo", null, function(data) {
                window.alert("定位信息：" + data);

            })
        })

    }



    function writeUserInfo() {

        setupWebViewJsBridge(function(bridge) {
            let params = {
                "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJqd3RfeWgiLCJleHAiOjE3NTgzMzA5NTQsInN1YiI6IllIIiwiYXVkIjoiZXZlcnkiLCJuYmYiOjE3MjY3OTQ5NTQsImlhdCI6MTcyNjc5NDk1NCwianRpIjoxMDAwMSwidWlkIjozfQ.0Za5Ig3BpEV2S1igIReAN6JlXXswrtmCpBjRYy2W3IQ",
                "avatar": "http://47.238.171.21:8100/uploads/images/user/20221002/dc2b48ed57778e10148.png",
                "username": "cbuser",
                "email": "cbuser@163.com"
            }
            bridge.callHandler("writeUserInfo", params, function(data) {
                window.alert("写入用户信息"+data);
            })
        })


    }

    function getUserInfo() {

        setupWebViewJsBridge(function(bridge) {

            bridge.callHandler("getUserInfo", null, function(data) {

                document.getElementById("text").innerHTML = JSON.stringify(data);
            })
        })


    }

    function writBusTruck() {

        setupWebViewJsBridge(function(bridge) {
            let baseInfo = {
                "id": 79,
                "register_time": "18/09/2024 21:11",
                "truck_machine_id": 16,
                "dig_machine_id": 1,
                "project_id": 22,
                "employ_id": 0,
                "digging_from": "",
                "block_id": 0,
                "destination_zone": "",
                "create_time": "01/01/1970 00:33:44",
                "update_time": "01/01/1970 00:33:44",
                "mark": 1,
                "material_id": 0,
                "dig_plan_id": 0,
                "rel_dig_plan_id": 0,
                "project": "Mid-West Region",
                "truck_machine_name": "BN-RD106",
                "dig_machine_name": "BN-EX01",
                "material_name": "",
                "block_name": "",
                "dig_plan_info": {
                    "label_vc": "SHOT-",
                    "project": "",
                    "blocks": [],
                    "title": "BDN-JI1 -",
                    "destination": "",
                    "materials": []
                },
                "employee_name": "",
                "action": 0,
                "standby": 0,
                "status": 0,
                "tipping": 0,
                "weight": "75"
            }

            let params = {
                "username": "cbuser",
                "truck_load_id": baseInfo.id,
                "data": JSON.stringify(baseInfo),
                "daytime": baseInfo.register_time.split(" ")[0]
            }
            bridge.callHandler("writBusTruck", params, function(data) {

                window.alert("写入结果："+data);
            })
        })


    }

  function readBusTruck() {

			let parmas = {
				"daytime": "18/09/2024",
				"currentTruckLoadId": 79
			}
			setupWebViewJsBridge(function(bridge) {

				bridge.callHandler("getBusTruck", parmas, function(data) {

					document.getElementById("text").innerHTML = JSON.stringify(data);
				})
			})


		}


    function writeLogInfo() {

        setupWebViewJsBridge(function(bridge) {


            let baseInfo = {
                "status": 1,
                "start_hour": "3",
                "truck_load_id": 79,
                "lng": null,
                "lat": null,
                "address": ""
            }

            let params = {
                "username": "cbuser",
                "truck_id": baseInfo.id,
                "data": JSON.stringify(baseInfo),
                "insert_time": (new Date()).valueOf()
            }

            bridge.callHandler("writeTruckLog", params, function(data) {
                window.alert("写入日志："+data);

            })
        })


    }

    function startUpdate() {

        setupWebViewJsBridge(function(bridge) {

            bridge.callHandler("startUpdate", null, function(data) {

            })
        })


    }


    function delayedRegisterHandler() {
        setupWebViewJsBridge(function(bridge) {
            bridge.registerHandler("delayedRegisterAlert", function(data, callback) {
                let result = window.confirm(
                    "点击通知信息:" + data
                )
                if (result) {
                    callback({
                        "result": "confirm"
                    })
                } else {
                    callback({
                        "result": "cancel"
                    })
                }
            })
        })
    }
</script>
</html>